<?php
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('msfrmproducttype_loadGrid');      
$objXajax->registerFunction('msfrmproducttype_save');
$objXajax->registerFunction('msfrmproducttype_edit');
$objXajax->registerFunction('msfrmproducttype_delete');

function msfrmproducttype_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse = new xajaxResponse();
    //$hotel_id = $_SESSION['current']['hotel_id'];   
    //$user_id = $_SESSION['current']['user_id'];        
    $name_vi = addslashes($params['txtnamemenuVi']);
    $name_en = addslashes($params['txtnamemenuEn']);
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit==1){
        $sql .="UPDATE ka_product$tbl_postfix SET name_vi = '$name_vi',name_en = '$name_en',
                user_update = '$user_id',date_update = now() WHERE id ='$id'";       
        $objDbSelect->Execute($sql);
        
    }else{
        $sql.= " INSERT INTO ka_product$tbl_postfix(name_vi,name_en,product_type,active,user_created,date_created,user_update,date_update)
        VALUES('$name_vi','$name_en',0,'1','$user_id',now(),'$user_id',now())"; 
        $objDbSelect->Execute($sql); 
        $parent_id = mysql_insert_id();
        $sql = " UPDATE ka_product$tbl_postfix SET parent_id = '$parent_id' WHERE id = $parent_id";
        $objDbSelect->Execute($sql);        

    }    
    $objResponse->addScript('xajax_msfrmproducttype_loadGrid();');
  //  $objResponse->addScript('xajax_kahtfrmmanagerhotel_loadMenu();');
  //  $objResponse->addScript('$( "#kahtfrmmanagerhotel_leftmenu" ).accordion();');
    
    
    return $objResponse->getXML();
}

function msfrmproducttype_loadGrid()
{     

    global $lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = msfrmproducttype_getGrid();    
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/ka_producttype_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('msfrmproducttype_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('msfrmproducttype_tbl');"); 
     $objResponse->addScript('changeWaiting(1);');                             
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function msfrmproducttype_getGrid()
{
    global $objDbSelect,$lang,$tbl_postfix;
    $sqlSelect = " select * from ka_product$tbl_postfix as pt1 WHERE  product_type = 0 order by pt1.name_$lang ";
    return $objDbSelect->GetArray($sqlSelect);
}  

    function msfrmproducttype_edit($id){
        global $objDbSelect,$tbl_postfix;
        $objResponse = new xajaxResponse();
        $sql = " SELECT * FROM ka_product$tbl_postfix WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        $name_vi   = $arr[0]['name_vi'];
        $name_en   = $arr[0]['name_en'];      
        $objResponse->addScript("$('#msfrmproducttype_txtnamemenuVi').val('$name_vi');");
        $objResponse->addScript("$('#msfrmproducttype_txtnamemenuEn').val('$name_en');");
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
    function msfrmproducttype_delete($id){             
        global $objDbSelect,$tbl_postfix;
        $sql = "delete from ka_product$tbl_postfix where parent_id = '$id' ";
        $result = $objDbSelect->Execute($sql);
        $objResponse = new xajaxResponse();
         $objResponse->addScript('xajax_msfrmproducttype_loadGrid();');
       // $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();
    }
    
?>
